<% include ../common/header.ejs %>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <% include nav.ejs %>

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>发布</legend>
            </fieldset>
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">

                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">奖励</label>
                    <div class="layui-input-block">
                        <input name="reward" type="text" name="title" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">

                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">难度</label>
                    <div class="layui-input-block">
                        <div id="test1"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">人数</label>
                    <div class="layui-input-block">
                        <input name="num" type="number" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">截止</label>
                    <div class="layui-input-block">
                        <input name="expiration" type="text" class="layui-input" id="test2" placeholder="yyyy-MM-dd">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block">
                        <textarea id="demo" style="display: none;"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>

                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script>
    //JavaScript代码区域
    layui.use(['element','table','form','rate','laydate','layedit'], function() {
        var form = layui.form,
            rate = layui.rate,
            laydate = layui.laydate,
            layedit = layui.layedit;

        var grade;
        //渲染
         rate.render({
            elem:"#test1" ,//绑定元素
             choose: function(value){
                 grade = value;
             }
        });

        laydate.render({
            elem:"#test2"

        });
        var edit = layedit.build('demo',{
            //保存文件过程
            //1.文件上传给后台
            //2.后台把文件保存到服务器硬盘
            //3.img src = 路径
            uploadImage:{url:'/api/upload',type:'post'}
        });

        form.on('submit(formDemo)', function(data){
            var da = data.field;
            da.difficulty = grade;
            da.content = layedit.getContent(edit)
            layui.$.ajax({
                url:'/admin/task/add',
                method:'post',
                data:da,
                success(data){
                    if(data.code === 0){
                        layer.alert(data.data);
                        location.reload()
                    }
                }
            })
            return false;
        });

    });
</script>

</body>
</html>
